package com.xcc.redis.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * {@like @ConditionalOnClass:
 * This annotation indicates that there must be RedisOperations in the current classpath to inject this Bean}
 *
 * @ConditionalOnClass(Jedis.class) 此注解表示当前ClassPath必须包含有Jedis这个类才会入这个配置类到spring容器中
 * 意思就是项目当中存在了jedis客户端依赖才觉得你需要使用，否则就没必要去注入.
 */
@Configuration
@ConditionalOnClass(Jedis.class)
public class MyRedisConfiguration {

    // 加载配置文件信息 这里使用properties类去做配置加载。
    // @src = com.xcc.redis.config.MyRedisProperties

    /**
     * <!-- 1、配置jedis连接池信息 -->
     * <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
     * <property name="maxTotal" value="50"></property>
     * <property name="maxIdle" value="5"></property>
     * </bean>
     */
    @Bean
    public JedisPool jedisPool(MyRedisProperties redisProperties) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
        jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
        jedisPoolConfig.setMaxTotal(redisProperties.getMaxTotal());
        String password = isBlank(redisProperties.getPassword()) ? null : redisProperties.getPassword();

        return new JedisPool(jedisPoolConfig, redisProperties.getHostName(), redisProperties.getPort(), redisProperties.getTimeout(), password);
    }

    /**
     * <!--2、配置连接工厂JedisConnectionFactory-->
     * <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
     * <!-- 服务器地址 -->
     * <property name="hostName" value="127.0.0.1"></property>
     * <!-- 服务端口号 -->
     * <property name="port" value="6379"></property>
     * <!-- 密码 -->
     * <property name="password" value="yichun"></property>
     * <!-- 连接池配置：再把第一步配置好的连接池信息通过属性注入进来 否则会采用默认的连接池-->
     * <property name="poolConfig" ref="jedisPoolConfig"></property>
     * </bean>
     */
    @Bean
    public JedisConnectionFactory redisConnectionFactory(MyRedisProperties redisProperties) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(redisProperties.getHostName());
        jedisConnectionFactory.setPort(redisProperties.getPort());
        jedisConnectionFactory.setPassword(redisProperties.getPassword());
        jedisConnectionFactory.setDatabase(redisProperties.getDbIndex());
        return jedisConnectionFactory;
    }

    // 第三步抽离出：@src = org.xing.modules.template.RedisTemplateConfiguration

    /**
     * <!--  3、配置RedisTemplate模板  把第二步配置好的连接工厂JedisConnectionFactory通过属性注入到RedisTemplate模板中-->
     * <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
     * <property name="connectionFactory" ref="jedisConnectionFactory"></property>
     * <p>
     * <!-- 配置一些key和value的序列化操作，可选操作 -->
     * <property name="keySerializer">
     * <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
     * </property>
     * <property name="valueSerializer">
     * <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
     * </property>
     * </bean>
     */

    public static boolean isBlank(String str) {
        return str == null || "".equals(str.trim());
    }
}
